import { createStore } from 'vuex'
const defaultTodo = [
  {
    id: 1,
    title: '吃饭',
    isCompleted: false
  },
  {
    id: 2,
    title: '睡觉',
    isCompleted: true
  }
]
export default createStore({
  state: {
    list: JSON.parse(sessionStorage.getItem('todo-list')) || defaultTodo
  },
  mutations: {
    addTodo(state, params) {
      state.list.push(params)
      sessionStorage.setItem('todo-list', JSON.stringify(state.list))
    },
    deleteTodo(state, params) {
      state.list.splice(params, 1)
      sessionStorage.setItem('todo-list', JSON.stringify(state.list))
    },
    checkedTodo(state, params) {
      let isCompleted = state.list[params].isCompleted
      isCompleted = !isCompleted
      sessionStorage.setItem('todo-list', JSON.stringify(state.list))
    },
    cleanCompleted(state, params) {
      state.list = params
      sessionStorage.setItem('todo-list', JSON.stringify(state.list))
    }
  },
  actions: {
  },
  modules: {
  }
})
